package com.nosnow.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nosnow.edu.entity.*;
import com.nosnow.edu.entity.vo.CourseInfoVo;
import com.nosnow.edu.entity.vo.CoursePublishVo;
import com.nosnow.edu.entity.vo.front.CourseFrontVo;
import com.nosnow.edu.entity.vo.front.CourseWebVo;
import com.nosnow.edu.mapper.EduChapterMapper;
import com.nosnow.edu.mapper.EduCourseDescriptionMapper;
import com.nosnow.edu.mapper.EduCourseMapper;
import com.nosnow.edu.mapper.EduVideoMapper;
import com.nosnow.edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nosnow.edu.service.EduVideoService;
import com.nosnow.handler.SnowflakeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author bingheng
 * @since 2020-04-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionMapper eduCourseDescriptionMapper;

    @Autowired
    private EduVideoMapper eduVideoMapper;

    @Autowired
    private EduChapterMapper eduChapterMapper;

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        // 1. 向课程基本信息表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int flog = baseMapper.insert(eduCourse);
        if(flog <= 0) {
            throw new SnowflakeException(20001, "添加课程基本信息失败");
        }

        // 2. 向课程简介表添加课程简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionMapper.insert(eduCourseDescription);

        return eduCourse.getId();
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();

        // 1. 查询课程基本信息表信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        // 2. 查询课程描述表信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionMapper.selectById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {

        // 1. 修改课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int flog = baseMapper.updateById(eduCourse);
        if(flog <= 0) {
            throw new SnowflakeException(20001, "修改课程基本信息失败");
        }

        // 2. 修改课程描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionMapper.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public Boolean deleteCourse(String courseId) {
        // 1. 根据课程Id删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        // 2. 根据课程Id删除章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        eduChapterMapper.delete(wrapperChapter);

        // 3. 根据课程Id删除描述
        eduCourseDescriptionMapper.deleteById(courseId);

        // 4. 根据课程Id删除课程本身
        int flog = baseMapper.deleteById(courseId);
        if(flog == 0) {
            throw new SnowflakeException(20001, "删除课程失败");
        }
        return flog > 0;
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> page, CourseFrontVo courseFrontVo) {
        // 1. 封装查询条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        // 2. 查询条件：非空则进行查询
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }

        // 3. 排序条件：非空则进行排序
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }

        // 4. 查询数据
        baseMapper.selectPage(page, wrapper);

        // 5. 将分页数据放入到map中
        Map<String, Object> map = new HashMap<>();
        map.put("items", page.getRecords());
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.selectBaseCourseInfo(courseId);
    }
}
